<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 命令- killall | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 命令- killall</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Linux系统中的killall命令用于杀死指定名字的进程（kill processes by name）。我们可以使用kill命令杀死指定进程PID的进程，如果要找到我们需要杀死的进程，我们还需要在之前使用ps等命令再配合grep来查找进程，而killall把这两个过程合二为一，是一个很好用的命令。</p>
<h2 id="1．命令格式"><a href="#1．命令格式" class="headerlink" title="1．命令格式"></a>1．命令格式</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">killall [参数] [进程名]</div></pre></td></tr></table></figure>
<h2 id="2．命令功能"><a href="#2．命令功能" class="headerlink" title="2．命令功能"></a>2．命令功能</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用来结束同名的的所有进程</p>
<h2 id="3．命令参数"><a href="#3．命令参数" class="headerlink" title="3．命令参数"></a>3．命令参数</h2><ul>
<li>-Z 只杀死拥有scontext 的进程</li>
<li>-e 要求匹配进程名称</li>
<li>-I 忽略小写</li>
<li>-g 杀死进程组而不是进程</li>
<li>-i 交互模式，杀死进程前先询问用户</li>
<li>-l 列出所有的已知信号名称</li>
<li>-q 不输出警告信息</li>
<li>-s 发送指定的信号</li>
<li>-v 报告信号是否成功发送</li>
<li>-w 等待进程死亡</li>
<li>–help 显示帮助信息</li>
<li>–version 显示版本显示</li>
</ul>
<h2 id="4．使用实例"><a href="#4．使用实例" class="headerlink" title="4．使用实例"></a>4．使用实例</h2><h3 id="实例1：杀死所有同名进程"><a href="#实例1：杀死所有同名进程" class="headerlink" title="实例1：杀死所有同名进程"></a>实例1：杀死所有同名进程</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">killall vi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># ps -ef|grep vi</span></div><div class="line">root     17581 17398  0 17:51 pts/0    00:00:00 vi test.txt</div><div class="line">root     17611 17582  0 17:51 pts/1    00:00:00 grep vi</div><div class="line">[root@localhost ~]<span class="comment"># ps -ef|grep vi</span></div><div class="line">root     17581 17398  0 17:51 pts/0    00:00:00 vi test.txt</div><div class="line">root     17640 17612  0 17:51 pts/2    00:00:00 vi test.log</div><div class="line">root     17642 17582  0 17:51 pts/1    00:00:00 grep vi</div><div class="line">[root@localhost ~]<span class="comment"># killall vi</span></div><div class="line">[root@localhost ~]<span class="comment"># ps -ef|grep vi</span></div><div class="line">root     17645 17582  0 17:52 pts/1    00:00:00 grep vi</div></pre></td></tr></table></figure>
<h3 id="实例2：向进程发送指定信号"><a href="#实例2：向进程发送指定信号" class="headerlink" title="实例2：向进程发送指定信号"></a>实例2：向进程发送指定信号</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;后台运行程序：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vi &amp;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;杀死 vi进程：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">killall -TERM vi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">killall -KILL vi</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vi &amp; </span></div><div class="line">[1] 17646[root@localhost ~]<span class="comment"># killall -TERM vi</span></div><div class="line">[1]+  Stopped                 vi</div><div class="line">[root@localhost ~]<span class="comment"># vi &amp; </span></div><div class="line">[2] 17648[root@localhost ~]<span class="comment"># ps -ef|grep vi</span></div><div class="line">root     17646 17582  0 17:54 pts/1    00:00:00 vi</div><div class="line">root     17648 17582  0 17:54 pts/1    00:00:00 vi</div><div class="line">root     17650 17582  0 17:55 pts/1    00:00:00 grep vi</div><div class="line">[2]+  Stopped                 vi</div><div class="line">[root@localhost ~]<span class="comment"># killall -TERM vi</span></div><div class="line">[root@localhost ~]<span class="comment"># ps -ef|grep vi</span></div><div class="line">root     17646 17582  0 17:54 pts/1    00:00:00 vi</div><div class="line">root     17648 17582  0 17:54 pts/1    00:00:00 vi</div><div class="line">root     17653 17582  0 17:55 pts/1    00:00:00 grep vi</div><div class="line">[root@localhost ~]<span class="comment"># killall -KILL vi</span></div><div class="line">[1]-  已杀死               vi</div><div class="line">[2]+  已杀死               vi</div><div class="line">[root@localhost ~]<span class="comment"># ps -ef|grep vi</span></div><div class="line">root     17656 17582  0 17:56 pts/1    00:00:00 grep vi</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例3：把所有的登录后的shell给杀掉"><a href="#实例3：把所有的登录后的shell给杀掉" class="headerlink" title="实例3：把所有的登录后的shell给杀掉"></a>实例3：把所有的登录后的shell给杀掉</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">killall -9 bash</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># w</span></div><div class="line"> 18:01:03 up 41 days, 18:53,  3 users,  load average: 0.00, 0.00, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT</div><div class="line">root     pts/0    10.2.0.68        14:58    9:52   0.10s  0.10s -bash</div><div class="line">root     pts/1    10.2.0.68        17:51    0.00s  0.02s  0.00s w</div><div class="line">root     pts/2    10.2.0.68        17:51    9:24   0.01s  0.01s -bash</div><div class="line">[root@localhost ~]<span class="comment"># killall -9 bash</span></div><div class="line">[root@localhost ~]<span class="comment"># w</span></div><div class="line"> 18:01:48 up 41 days, 18:54,  1 user,  load average: 0.07, 0.02, 0.00USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT</div><div class="line">root     pts/0    10.2.0.68        18:01    0.00s  0.01s  0.00s w</div><div class="line">[root@localhost ~]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;运行命令：killall -9 bash 后，所有bash都会被卡掉了，所以当前所有连接丢失了。需要重新连接并登录。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/45. Linux 命令- killall/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
